home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / Networking / AppleTalk Libraries / TestLib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-03  |  11.6 KB  |  411 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    TestLib.c : MPW tool to test  3 librairies : ZIP - ATP - AFP
  4. #
  5. #    Zone names are displayed
  6. #    Enter a zone name if any and validate your answer by the Enter key
  7. #    Server names are displayed
  8. #    Enter a server name and validate your answer by the Enter key
  9. #    Logging as guest or with username/password is completed
  10. #    Server time as well as volume names are displayed
  11. #    Enter a volume name and validate your answer by the Enter key
  12. #    Volume access rights are displayed in guest mode
  13. #    For each directory accessible, access rights are displayed
  14. #
  15. #    Versions:    1.0                    10/91
  16. #    Built with MPW 3.2
  17. #
  18. #    C.Buttin - Apple Computer Europe            
  19. #
  20. ------------------------------------------------------------------------------*/
  21.  
  22. #include <Memory.h>
  23. #include <Types.h>
  24. #include <stdIO.h>
  25. #include <String.h>
  26. #include <Strings.h>
  27. #include <errors.h>
  28. #include <OSUtils.h>
  29. #include <AppleTalk.h>
  30.  
  31. #define buffSize    5000
  32. #define maxServers    40
  33.  
  34. /* structures  used by ExtractDirInfo */
  35. typedef struct InfoDir {
  36.     short     UAM;                /* access right for this directory */
  37.     long     dirID;                
  38.     Str32    dirName;
  39. }InfoDir;
  40.  
  41. typedef InfoDir *InfoDirPtr;
  42.  
  43.  
  44. /* functions */
  45. short    DisplayZones(Ptr buffer);
  46. short    DisplayServers(Ptr zoneName,Ptr buffer);
  47. Boolean GetServerAddress(Ptr theServer,AddrBlock* serverAddress,short numEntities,Ptr buffer);
  48. short    LogOnServer(AddrBlock* serverAddress,Ptr SCBBlock,Ptr buffer);
  49. void    DisplayVolumes(Ptr buffer,short numVol);
  50. void    DisplayDirectories(short sessionNum,short volumeID,long dirID);
  51. void    getAnswer(Ptr theString);
  52. void     DisplayAccessRights(short accessRights);
  53. void    padWithBlanks(Str32 theStr);
  54.  
  55. /* ZIP */
  56. extern pascal short      GetZones(Ptr buffer,short bufsize);
  57. extern pascal void           GetZoneName(char* buffer,short zoneNumber,char* zoneName);
  58. /* NBP */
  59. extern pascal short       NBPGetList(Ptr buffer,
  60.                                short bufSize,
  61.                                EntityPtr entity,
  62.                                short maxEntities);
  63. extern pascal Boolean       NBPGetAddress(Ptr buffer,
  64.                                   short tuplenum,
  65.                                   short numEntities,
  66.                                   EntityPtr entity,
  67.                                   AddrBlock *address);
  68. /* AFP */
  69. extern pascal OSErr     GetServerInfo(AddrBlock *serverAddress,Ptr replyBuffer,short buffLength);
  70. extern pascal short        LogOnwithName(AddrBlock *serverAddress,Ptr theName,Ptr thePassword,Ptr SCBBlock);
  71. extern pascal short     LogOnAsGuest(AddrBlock *serverAddress,Ptr SCBBlock);
  72. extern pascal OSErr     GetServerParams(short sessNum,Ptr replyBuffer,short buffLength);
  73. extern pascal unsigned long GetServerTime(Ptr replyBuffer);
  74. extern pascal short        GetNumberVolumes(Ptr replyBuffer);
  75. extern pascal Boolean     ExtractVolumeName(Ptr replyBuffer,short volumeNumber,Str255 theVol);
  76. extern pascal short     OpenVolume(short sessNum,Ptr volumeName,short* volID);
  77. extern pascal void         CloseVolume(short sessNum,short volumeID);
  78. extern pascal short     GetVolumePrivileges(short sessionNum,short volumeID);
  79. extern pascal OSErr     GetDirectories(short sessNum,short volID,long dirID,
  80.                                    Ptr buffer,short buffLength,short reqCount);
  81. extern pascal short     GetNumberDirs(Ptr replyBuffer);
  82. extern pascal Boolean     ExtractDirInfo(Ptr replyBuffer,short dirNumber,InfoDirPtr theDir);
  83. extern pascal OSErr     LogOut(short sessNum,Ptr SCBBlock);
  84. extern pascal Boolean    CheckUAM(const Str32 theMethod,Ptr replyBuffer);
  85.  
  86.  
  87. main()
  88. {
  89.     short         sessionNum,numVol,numEntities;
  90.     short        volumeID,result;
  91.     Ptr            buffer;
  92.     Str32        zoneName,theServer,theVol;
  93.     AddrBlock     serverAddress;
  94.     char        SCBBlock[scbMemSize];   /* used by AFP to manage a session */
  95.     OSErr        error;
  96.     short        accessRights;
  97.     DateTimeRec theDate;
  98.     
  99.     /* allocate a buffer to retrieve info */
  100.     if (!(buffer = NewPtr(buffSize))) {
  101.         printf("buffer too small");
  102.         return;
  103.         }
  104.     /* Display list of zones */
  105.     if ((result = DisplayZones(buffer)) < 0)  {    /* an error occured */
  106.         DisposePtr(buffer);
  107.         return;
  108.         }
  109.     else 
  110.         if (result >0) {
  111.             /* get selected zone */
  112.             printf("\n\nselect a zone : \n");
  113.             getAnswer(zoneName);
  114.             if (*zoneName == 0)                    /* no zone selected */
  115.                 return;
  116.             }
  117.         else                                    /* no zone available */
  118.             sprintf(zoneName,"\p*");
  119.         
  120.     /* get AppleShare servers in that zone */
  121.     if ((numEntities = DisplayServers(c2pstr(zoneName), buffer)) == 0)    { /* an error occured */
  122.         DisposePtr(buffer);
  123.         return;
  124.         }
  125.         
  126.     /* Get selected server and look for its address */
  127.     printf("\nGive the server name :\n");
  128.     getAnswer(theServer);
  129.     if (!GetServerAddress(theServer,&serverAddress,numEntities,buffer))    {    /* an error occured */
  130.         DisposePtr(buffer);
  131.         return;
  132.         }
  133.         
  134.     /* Connect to server */
  135.     if ((sessionNum = LogOnServer(&serverAddress,&SCBBlock,buffer)) <= 0)    {        /* an error occured */
  136.         DisposePtr(buffer);
  137.         return;
  138.         }
  139.         
  140.     /* get server information and volume list */
  141.     if ((error = GetServerParams(sessionNum,buffer,buffSize)) != noErr)
  142.             printf("Error in getting server params : %d\n",error);
  143.     else {
  144.         Secs2Date(GetServerTime(buffer),&theDate);
  145.         printf("Server Time : %d/%d/%d \t%d:%d:%d\n", theDate.day,theDate.month,theDate.year,
  146.                 theDate.hour,theDate.minute,theDate.second);
  147.                 
  148.         numVol = GetNumberVolumes(buffer);
  149.         if (numVol > 0) {
  150.             DisplayVolumes(buffer,numVol);
  151.             /* Get a volume name and open it */
  152.             printf("\nGive the volume name :\n");
  153.             getAnswer(theVol);
  154.             if (*theVol == 0)                    /* no volume selected */
  155.                 return;
  156.             if ((error = OpenVolume(sessionNum,c2pstr(theVol),&volumeID)) == noErr)    {
  157.                     accessRights = GetVolumePrivileges(sessionNum,volumeID);
  158.                     printf("\nVolume : %s ",p2cstr(theVol));
  159.                     DisplayAccessRights(accessRights);
  160.                     DisplayDirectories(sessionNum,volumeID,2);        /* dirID of the root is 2 */
  161.                 CloseVolume(sessionNum,volumeID);
  162.                 }
  163.             else
  164.                 printf("Can't open volume:%d",error);
  165.             }
  166.         }
  167.     
  168.     LogOut(sessionNum,&SCBBlock);
  169.     DisposePtr(buffer);
  170. }
  171.  
  172. /* DisplayZones : display all the zone names.
  173.    return : -1 an error occured
  174.              0 no zone available
  175.              1 list of zones */
  176.              
  177. short DisplayZones(Ptr buffer)
  178. {
  179.     short         i,numZones;
  180.     Str32        zoneName;
  181.     
  182.     /* get zone list */
  183.     if ((numZones = GetZones(buffer,buffSize)) < 0)    {
  184.         printf("Error in getting zones : %d\n",numZones);
  185.         return -1;
  186.         }
  187.     else    {
  188.         if (numZones == 0) {
  189.             printf("\nNo Zone available");
  190.             return 0;
  191.             }
  192.         else    {
  193.             for (i = 1; i <= numZones;i++) {
  194.                 GetZoneName(buffer,i,zoneName);
  195.                 padWithBlanks(p2cstr(zoneName));            
  196.                 printf("\t\t%s",zoneName);
  197.                 if (i % 3 == 0)
  198.                     printf("\n");
  199.                 }
  200.             return 1;
  201.             }
  202.         }
  203. } /* DisplayZones */
  204.  
  205. short DisplayServers(Ptr zoneName,Ptr buffer)
  206. {
  207.     short         i,numEntities;
  208.     AddrBlock     serverAddress;
  209.     EntityName    entity;
  210.  
  211.  
  212.     /* prepare the entities we are looking for */
  213.     NBPSetEntity((Ptr)&entity,"\p=","\pAFPServer",zoneName);
  214.     if ((numEntities = NBPGetList(buffer,buffSize,&entity,maxServers)) <= 0)    {
  215.         printf("Error in getting entities :%d",numEntities);
  216.         return 0;
  217.         }
  218.     
  219.     /* list AppleShare servers */
  220.     printf("\n");
  221.     for (i = 1; i <= numEntities ; i++) 
  222.         if (NBPGetAddress(buffer,i,numEntities,&entity,&serverAddress))    {
  223.             /* pad server name with blanks to display nicely */
  224.             padWithBlanks(p2cstr(entity.objStr));
  225.             printf("Server :\t%s\t\tZone : \t%s\n",entity.objStr,
  226.                     p2cstr(entity.zoneStr));
  227.                 }
  228.     return numEntities;
  229. }    /* DisplayServers */
  230.  
  231. Boolean GetServerAddress(Ptr theServer,AddrBlock* serverAddress,short numEntities,Ptr buffer)
  232. {
  233.     short         i;
  234.     EntityName    entity;
  235.  
  236.     for (i = 1; i <= numEntities ; i++) {
  237.         NBPGetAddress(buffer,i,numEntities,&entity,serverAddress);
  238.         if (strcmp(p2cstr(entity.objStr),theServer) == 0)
  239.             break;
  240.         }
  241.     if (i > numEntities) {
  242.         printf("Server not found\n");
  243.         return false;
  244.         }
  245.     printf("Address : \tNet : %ld\tNode : %d\tSocket : %d\n",serverAddress->aNet,
  246.             serverAddress->aNode,
  247.             serverAddress->aSocket);
  248.     return true;
  249. }    /* GetServerAddress */    
  250.  
  251. /* establish a connection to the server */
  252. short LogOnServer(AddrBlock* serverAddress,Ptr SCBBlock,Ptr buffer)
  253. {
  254.     short     sessionNum;
  255.     OSErr    error;
  256.     Str32    theName,temp;
  257.     char    thePassword[8];
  258.     Boolean connectAsGuest;
  259.     
  260.     connectAsGuest = true;    
  261.     /* look to connect with a username password by checking the UAMs */
  262.     if ((error = GetServerInfo(serverAddress,buffer,buffSize)) != noErr)    {
  263.         printf("Error in getting server Information : %d\n",error);
  264.         return 0;
  265.         }
  266.     
  267.     /* verify access methods */
  268.     connectAsGuest = true;
  269.     if (CheckUAM("\pCleartxt passwrd",buffer))    {
  270.         /* ask user if he wants to connect with user name password */
  271.         printf("Do you want to connect to the server as guest : 1 yes, 0 no ?\n");
  272.         getAnswer(temp);
  273.         if (strcmp(temp,"0") == 0)    {
  274.         connectAsGuest = false;
  275.         /* get username and password and log on */
  276.             printf("UserName :\n");
  277.             getAnswer(theName);
  278.             printf("Password :\n");
  279.             getAnswer(thePassword);
  280.             sessionNum = LogOnwithName(serverAddress,c2pstr(theName),c2pstr(thePassword),SCBBlock);
  281.             if (sessionNum > 0)
  282.                 printf("\n%s : Logging on server completed\n",p2cstr(theName));
  283.             }
  284.         }
  285.     if (connectAsGuest) {
  286.         /* connect to server as guest, if possible */
  287.         if (CheckUAM("\pNo User Authent",buffer))    {
  288.             sessionNum = LogOnAsGuest(serverAddress,SCBBlock);
  289.             if (sessionNum > 0)
  290.                 printf("\nLogging on server as guest completed\n");
  291.             }
  292.         else    {
  293.             printf("\nLogging on server as guest impossible\n");
  294.             sessionNum = 0;
  295.             }
  296.         }
  297.  
  298.     if (sessionNum < 0)
  299.         printf("Error in loging to server : %d\n",sessionNum);
  300.  
  301.     return sessionNum;
  302. } /* LogOnServer */
  303.  
  304.  
  305. void DisplayVolumes(Ptr buffer,short numVol)
  306. {
  307.     short         i;
  308.     Str32        theVol;
  309.     
  310.     printf("\nNumber of volumes : %d\n",numVol);
  311.     printf("Volumes :\n");
  312.     for (i = 1; i <= numVol;i++)
  313.         if (ExtractVolumeName(buffer,i,theVol))
  314.             printf("\t%s\n",p2cstr(theVol));
  315. }    /* DisplayVolumes */
  316.     
  317.  
  318. void DisplayDirectories(short sessionNum,short volumeID,long dirID)
  319. {
  320.     OSErr         error;
  321.     short         i,j,numDir;
  322.     InfoDir        theDir;
  323.     static short theTab = -1;
  324.     Ptr            buffer;
  325.     
  326.     /* allocate a buffer to receive the information */
  327.     if (!(buffer = NewPtr(buffSize))) {
  328.         printf("DisplayDirectories buffer too small\n");
  329.         return;
  330.         }
  331.     /* retrieve directory info */
  332.     if ((error = GetDirectories(sessionNum,volumeID,dirID,buffer,buffSize,20)) != noErr)    
  333.         if (error == afpObjectNotFound)    {    /* we have browse a whole branch */
  334.             DisposPtr(buffer);
  335.             return;
  336.             }
  337.                 
  338.     /* get number of directories */
  339.     if ((numDir = GetNumberDirs(buffer)) <= 0)
  340.         return;
  341.     
  342.     if (theTab < 0) {
  343.         printf("\nDirectories and access rights :\n");
  344.         theTab = 0;
  345.         }
  346.         
  347.     /* retrieve info about each directory, display it and go on recursively if we have access
  348.        rights */
  349.      for (i = 1; i <= numDir;i++)    {
  350.          /* extract info and print it */
  351.          if (ExtractDirInfo(buffer,i,&theDir)) {
  352.             for (j = 0; j < theTab;j++)
  353.                 printf("\t");
  354.             /* pad with Blanks to get a nice display */
  355.             padWithBlanks(p2cstr(theDir.dirName));
  356.             printf("%s  ",theDir.dirName);
  357.             DisplayAccessRights(theDir.UAM);
  358.  
  359.             /* look for subdirectories if possible */
  360.             if (theDir.UAM & 0xF1)        {
  361.                 theTab++;
  362.                 DisplayDirectories(sessionNum,volumeID,theDir.dirID);
  363.                 theTab--;
  364.                 }
  365.             }
  366.         }        
  367.     DisposPtr(buffer);
  368. }    /* DisplayDirectories */
  369.  
  370. /* get from user a C string */
  371. void getAnswer(Ptr theString)
  372. {
  373.     short i;
  374.     
  375.     i = 0;
  376.     do    {
  377.         scanf("%c",theString+i);    /* a zone name can contains white spaces */
  378.         i++;                        /* so we get it char by char */
  379.         }
  380.     while (*(theString+i-1) != '\n');
  381.     *(theString+i-1) = '\0';
  382. }    /* getAnswer */
  383.  
  384. void     DisplayAccessRights(short accessRights)
  385. {
  386.     Str255         theStr;
  387.  
  388.     *theStr = 0;
  389.     if (accessRights == 0)        /* no access to guest */
  390.         printf("No access for guest\n");
  391.     else {
  392.         if (accessRights & 0xF4)
  393.             strcat(theStr,"Make Changes ");
  394.         if (accessRights & 0xF2)
  395.             strcat(theStr,"See Files ");
  396.         if (accessRights & 0xF1)
  397.             strcat(theStr,"See Folders");
  398.         
  399.         printf("%s\n",theStr);
  400.         }
  401.  
  402. }    /* DisplayAccessRights */
  403.  
  404. /* pad text with blanks to display nicely */
  405. void padWithBlanks(Str32 theStr)
  406. {
  407.     short i;
  408.     
  409.     for (i = strlen(theStr); i < 31;theStr[i] = ' ',i++);
  410.     theStr[31] = '\0';
  411. } /* padWithBlanks */